package com.scau402.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.scau402.common.exception.ServiceException;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.system1.cipher.CipherEnum;
import com.scau402.system1.cipher.CipherHandler;
import com.scau402.system.domain.SysCiphertext;
import com.scau402.system.mapper.SysCiphertextMapper;
import com.scau402.system.service.ISysCiphertextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 暗码Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-27
 */
@Service
public class SysCiphertextServiceImpl implements ISysCiphertextService {
    @Autowired
    private SysCiphertextMapper sysCiphertextMapper;

    /**
     * 查询暗码
     *
     * @return 暗码
     */
    @Override
    public SysCiphertext selectSysCiphertextByTenantId(Long typeId) {
        Long tenantId = SecurityUtils.getLoginUser().getTenantId();
        if (typeId == null) {
            throw new ServiceException("typeId参数缺失");
        }
        return sysCiphertextMapper.selectOne(
                new LambdaQueryWrapper<SysCiphertext>()
                        .eq(SysCiphertext::getSysTenantId, tenantId)
                        .eq(SysCiphertext::getTypeId, typeId)
        );
    }

    /**
     * 新增暗码
     *
     * @param sysCiphertext 暗码
     * @return 结果
     */
    @Override
    public int insertSysCiphertext(SysCiphertext sysCiphertext) {
        //赋值租户id信息
        sysCiphertext.setSysTenantId(SecurityUtils.getLoginUser().getTenantId());
        CipherHandler handler = CipherEnum.getByTypeId(sysCiphertext.getTypeId()).getHandler();
        handler.checkIsValid(sysCiphertext.getConfig());
        return sysCiphertextMapper.insert(sysCiphertext);
    }

    /**
     * 修改暗码
     *
     * @param sysCiphertext 暗码
     * @return 结果
     */
    @Override
    public int updateSysCiphertext(SysCiphertext sysCiphertext) {
        CipherHandler handler = CipherEnum.getByTypeId(sysCiphertext.getTypeId()).getHandler();
        handler.checkIsValid(sysCiphertext.getConfig());
        if (sysCiphertext.getStatus() == 1) {
            Long tenantId = SecurityUtils.getLoginUser().getTenantId();
            sysCiphertextMapper.update(
                    null,
                    new LambdaUpdateWrapper<SysCiphertext>()
                            .eq(SysCiphertext::getSysTenantId, tenantId)
                            .set(SysCiphertext::getStatus, 0)
            );
        }
        return sysCiphertextMapper.updateById(sysCiphertext);
    }

    /**
     * 明码转暗码
     *
     * @param price 价格明码
     * @return 暗码
     */
    @Override
    public String changePlaintextToCiphertext(String price) {
        Long tenantId = SecurityUtils.getLoginUser().getTenantId();
        SysCiphertext sysCiphertext = sysCiphertextMapper.selectOne(
                new LambdaQueryWrapper<SysCiphertext>()
                        .eq(SysCiphertext::getSysTenantId, tenantId)
                        .eq(SysCiphertext::getStatus, 1)
        );
        if (null == sysCiphertext) {
            throw new ServiceException("当前尚无启用中的暗码规则，请先前往暗码设置中设置");
        }
        CipherHandler handler = CipherEnum.getByTypeId(sysCiphertext.getTypeId()).getHandler();
        return handler.convert(sysCiphertext.getConfig(), price);
    }
}
